/*
 * @Author: 0x9DEFA478
 * @Date: 2021-08-27 16:56:20
 * @LastEditTime: 2021-10-04 17:17:12
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "GUI_User_Internal.h"
#include "Peripheral.h"

extern unsigned char GUI_User_OLED_Gram2[];

static H_TS_MessageQueue* volatile MoveQueue=NULL;


//使用该方法需保证: bdst与bsrc尺寸相同
static void BitmapCpy(H_Graphics_Bitmap* bdst,H_Graphics_Bitmap* bsrc){
  int i;
  unsigned int* src;
  unsigned int* dst;

  if((bdst->Height!=bsrc->Height)||(bdst->Width!=bsrc->Width)){
    return;
  }

  src=bsrc->Bitmap;
  dst=bdst->Bitmap;

  i=bdst->Width*bdst->Height/8;
  i/=sizeof(unsigned int);
  while(i>=8){
    i-=8;
    dst[0]=src[0];
    dst[1]=src[1];
    dst[2]=src[2];
    dst[3]=src[3];
    dst[4]=src[4];
    dst[5]=src[5];
    dst[6]=src[6];
    dst[7]=src[7];
    src=&src[8];
    dst=&dst[8];
  }
  while(i>0){
    i--;
    src=&src[1];
    dst=&dst[1];
  }


}

//使用该方法需保证: 1:bdst与bsrc尺寸相同 2:操作的图像不过界
static void BitmapCpy_Move(H_Graphics_Bitmap* bdst,H_Graphics_Bitmap* bsrc,GUI_User_Move* move){
  
  H_Graphics_Area_Param Param;
  int Image_Width;
  int Image_Height;


  Image_Width=bdst->Width;
  Image_Height=bdst->Height;

  Param.S=bsrc;
  Param.D=bdst;

  //顶部区域
  if((Image_Width>0)&&(move->Y>0)){
    Param.sX=0;
    Param.sY=0;
    Param.dX=0;
    Param.dY=0;
    Param.Width=Image_Width;
    Param.Height=move->Y;
    H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
  }
  
  //底部区域
  if((Image_Width>0)&&((move->Y+move->Height)<Image_Height)){
    Param.sX=0;
    Param.sY=move->Y+move->Height;
    Param.dX=0;
    Param.dY=Param.sY;
    Param.Width=Image_Width;
    Param.Height=Image_Height-Param.sY;
    H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
  }
  
  //左边区域
  if((move->Height>0)&&(move->X>0)){
    Param.sX=0;
    Param.sY=move->Y;
    Param.dX=0;
    Param.dY=Param.sY;
    Param.Width=move->X;
    Param.Height=move->Height;
    H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
  }

  //右边区域
  if((move->Height>0)&&((move->X+move->Width)<Image_Width)){
    Param.sX=move->X+move->Width;
    Param.sY=move->Y;
    Param.dX=Param.sX;
    Param.dY=Param.sY;
    Param.Width=Image_Width-Param.sX;
    Param.Height=move->Height;
    H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
  }

  //中心
  if((move->Height>0)&&(move->Width>0)&&(move->Process>0)){
    switch(move->Direction){
      case 0:
        Param.sX=move->X+move->Width-move->Process;
        Param.sY=move->Y;
        Param.dX=move->X;
        Param.dY=move->Y;
        Param.Width=move->Process;
        Param.Height=move->Height;
        H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
        break;
      case 90:
        Param.sX=move->X;
        Param.sY=move->Y;
        Param.dX=move->X;
        Param.dY=move->Y+move->Height-move->Process;
        Param.Width=move->Width;
        Param.Height=move->Process;
        H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
        break;
      case 180:
        Param.sX=move->X;
        Param.sY=move->Y;
        Param.dX=move->X+move->Width-move->Process;
        Param.dY=move->Y;
        Param.Width=move->Process;
        Param.Height=move->Height;
        H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
        break;
      case 270:
        Param.sX=move->X;
        Param.sY=move->Y+move->Height-move->Process;
        Param.dX=move->X;
        Param.dY=move->Y;
        Param.Width=move->Width;
        Param.Height=move->Process;
        H_Graphics_L1Vertical_CopyTo_L1Vertical(&Param);
        break;
      default:
        break;
    }
  }


}

//返回 0:还需渲染 其他无需渲染
static int MoveIsDone(GUI_User_Move* move){

  switch(move->Direction){
    case 0:
    case 180:
      if(move->Process<move->Width){
        move->Process+=move->Speed;
        if(move->Process>move->Width){
          move->Process=move->Width;
        }
        return 0;
      }
      break;
    case 90:
    case 270:
      if(move->Process<move->Height){
        move->Process+=move->Speed;
        if(move->Process>move->Height){
          move->Process=move->Height;
        }
        return 0;
      }
      break;
    default:
      break;
  }
  
  return -1;
}

//设置移动参数
void GUI_User_SetMove(GUI_User_Move* move){
  GUI_User_Move* new_move;

  if(MoveQueue==NULL){
    return;
  }

  new_move=MemoryMalloc_Malloc(sizeof(GUI_User_Move));
  if(new_move!=NULL){
    new_move[0]=move[0];

    if(0!=H_TS_MessageQueueSend(MoveQueue,new_move,(void*)MemoryMalloc_Free)){
      MemoryMalloc_Free(new_move);
    }
  }
  
}

//渲染图像到位图上
void GUI_User_Display(H_UI* h_ui,H_Graphics_Bitmap* bitmapDst){

  GUI_User_SetDrawStringBitmap(bitmapDst);

  if(h_ui==NULL){
    return;
  }
  H_UI_Page_Display(h_ui);

}

//刷屏线程
int Screen_Display_Thread(void* v){
  GUI_User_DisplayThreadParam Param;
  void* v0;
  void* v1;
  GUI_User_Move move;
  H_Graphics_Bitmap Gram;
  H_Graphics_Bitmap* Gram_In;
  void (*Free)(void*);

  MoveQueue=new_H_TS_MessageQueue(4);

  Gram.Bitmap=GUI_User_OLED_Gram2;
  Gram.Width=128;
  Gram.Height=64;

  move.Process=-1;

  Param=((GUI_User_DisplayThreadParam*)v)[0];

  for(;;){
    H_TS_MessageQueueReceive(Param.RecvQueue,&v0,&v1);
    //H_TS_SemaphoreGive(Param.RecvDone);//如果传入的是双缓存 可以提前释放信号量让其他线程开始渲染另一个缓存

    Gram_In=v0;

    if(0==H_TS_MessageQueueTryReceive(MoveQueue,&v0,&v1)){
      if(v0!=NULL){
        GUI_User_SetIsRespondButton(0);
        move=((GUI_User_Move*)v0)[0];
        Free=(void (*)(void*))v1;
        Free(v0);
      }
    }

    if(move.Process<0){
      BitmapCpy(&Gram,Gram_In);
    }else{
      if(MoveIsDone(&move)==0){
        BitmapCpy_Move(&Gram,Gram_In,&move);
      }else{
        move.Process=-1;
        GUI_User_SetIsRespondButton(-1);
        BitmapCpy(&Gram,Gram_In);
      }
    }

    H_TS_SemaphoreGive(Param.RecvDone);//如果外部只有有个缓存 复制完成后才释放信号量

    OLED_Display(Screen_OLED1,Gram.Bitmap);
  }
}






